package com.aiyige.utils.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aiyige.R;


public class SearchEditText extends RelativeLayout {

	private EditText editText;

	private ImageView deleteImage;

	private RelativeLayout deleteLayout;

	ActionCallback callback;

	public SearchEditText(Context context) {
		super(context);
		init(context);
	}


	public SearchEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
		setEditMaxLenth(context, attrs);
	}


	private void setEditMaxLenth(Context context, AttributeSet attrs) {
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyEditText);
		int editTextMaxLength = ta.getInteger(R.styleable.MyEditText_editMaxLength,Integer.MAX_VALUE);
		editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(editTextMaxLength)});
	}

	public SearchEditText(Context context, AttributeSet attrs, int defStyle){
		super(context, attrs, defStyle);
		init(context);
		setEditMaxLenth(context, attrs);

	}

	private void init(Context context) {
		LayoutInflater.from(context).inflate(R.layout.search_edittext, this);
		editText = (EditText) findViewById(R.id.change_value_edittext);
		deleteLayout = (RelativeLayout) findViewById(R.id.delete_edit_layout);
		deleteLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				editText.setText("");
			}
		});
		deleteImage = (ImageView) findViewById(R.id.delete_image);
		deleteImage.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				editText.setText("");
			}
		});
		editText.addTextChangedListener(inputWatcher);

	}
	/**
	 * EditText内容改变监听
	 */
	TextWatcher inputWatcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
								  int count) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
									  int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
			String password = editText.getText().toString();
			//当密码输入框为空时  隐藏清除icon，否则显示
			deleteLayout.setVisibility(TextUtils.isEmpty(password) ? View.GONE
					: View.VISIBLE);
		}
	};


	public void setEditTextHint(String hint) {
		if (editText != null && !TextUtils.isEmpty(hint)) {
			editText.setHint(hint);
		}
	}

	public void setSelection(int index) {
		if (editText != null) {
			editText.setSelection(index);
		}
	}

	public void setText(String value)
	{
		editText.setText(value);
	}

	public String getValue()
	{
		return editText.getText().toString();
	}


	public EditText getEditText() {
		return editText;
	}


	public void setActionSearch(final ActionCallback callback) {
		editText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
		editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);
		editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			public boolean onEditorAction(TextView v, int actionId,
										  KeyEvent event)  {
				if (actionId==EditorInfo.IME_ACTION_SEARCH
						||(event!=null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER)) {
					//do something;
					callback.actionSearch(editText.getText().toString().trim());
					return true;
				}
				return false;
			}
		});
	}

	public interface ActionCallback {
		void actionSearch(String value);
	}

}
